﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinApi;

namespace WenSkin.Controls
{
    [Designer(typeof(Design.Designer.WenTreeViewColumnDesigner))]
    public class WenTreeViewColumn : WenControl
    {
        public WenTreeViewColumn() : base()
        {
            AddControl();
        }

        #region 私有属性
        private ColumnHeaderCollection columns;
        private WenTreeView wenTreeView;


        private ColumnHeaderControl _columnHeaderControl;
        private ColumnHeaderControl columnHeaderControl => _columnHeaderControl ??= new ColumnHeaderControl(this);

        #endregion

        #region 公有属性

        [DefaultValue(null), Category("Wen"), Description("WenTreeView控件区域")]
        public WenTreeView WenTreeView => wenTreeView ??= new WenTreeView() { Dock = DockStyle.Fill };

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [DefaultValue(null), Category("Wen"), Description("列集合")]
        [Editor(typeof(Design.Editor.WenCollectionEditor), typeof(UITypeEditor))]
        public ColumnHeaderCollection Columns => columns ??= new ColumnHeaderCollection(this);

        [DefaultValue(30), Category("Wen"), Description("标题高")]
        public int HeaderHeight { get => WenTreeView.Height + 2; set => WenTreeView.Height = value + 2; }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [DefaultValue(null), Category("Wen"), Description("节点集合")]
        [Localizable(true)]
        [MergableProperty(false)]
        public TreeNodeCollection Nodes => WenTreeView.Nodes;

        [Browsable(false), DefaultValue(null)]
        public object DataSource { get => WenTreeView.DataSource; set => WenTreeView.DataSource = value; }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [DefaultValue(null), Category("Wen"), Description("绑定数据节点信息设置")]
        public TreeDataSourceNode TreeDataSourceNode => WenTreeView.TreeDataSourceNode;


        #endregion

        #region 继承或者子类继承属性

        [DefaultValue(30), Category("TreeView"), Description("单项高度")]
        public int ItemHeight { get => WenTreeView.ItemHeight; set => WenTreeView.ItemHeight = value; }

        [Category("TreeView")]
        [Description("绑定数据Text默认值")]
        [DefaultValue(null)]
        public string DefaultTextColumn { get => WenTreeView.DefaultTextColumn; set => WenTreeView.DefaultTextColumn = value; }

        [DefaultValue(false), Category("TreeView"), Description("是否可编辑标签")]
        public bool LabelEdit { get => WenTreeView.LabelEdit; set => WenTreeView.LabelEdit = value; }

        #endregion

        #region 继承或者子类重置委托

        public event TreeNodeMouseClickEventHandler NodeMouseClick;
        public event WenTreeView.TreeNodeDrawEventHandler TreeNodeDrag;
        public event NodeLabelEditEventHandler AfterLabelEdit;
        #endregion

        #region 继承或者子类重置方法函数

        public void CollapseAll()
        {
            WenTreeView.CollapseAll();
        }

        #endregion

        #region 重绘

        protected override void WenOnPaint(Graphics g, Rectangle rec, PaintEventArgs e)
        {
            base.WenOnPaint(g, rec, e);

            using Pen pen = new Pen(Color.FromArgb(199, 199, 199)) { Width = 1 };
            g.DrawRectangle(pen, new Rectangle(0, 0, Width - 1, Height - 1));
        }

        #endregion

        #region 添加控件
        private void AddControl()
        {
            this.Controls.Add(WenTreeView);
            this.Controls.Add(columnHeaderControl);
            WenTreeView.NodeMouseClick += (s, e) =>
            {
                NodeMouseClick?.Invoke(s, e);
            };
            wenTreeView.TreeNodeDrag += (s, e) =>
            {
                TreeNodeDrag?.Invoke(s, e);
            };            
            wenTreeView.AfterLabelEdit += (s, e) =>
            {
                AfterLabelEdit?.Invoke(s, e);
            };
        }

        #endregion
        private class ColumnHeaderControl : WenControl
        {
            private readonly WenTreeViewColumn owner;
            public ColumnHeaderControl(WenTreeViewColumn owner) : base()
            {
                this.owner = owner;
                Height = 32;
                Dock = DockStyle.Top;
            }

            protected override void WenOnPaint(Graphics g, Rectangle rec, PaintEventArgs e)
            {
                base.WenOnPaint(g, rec, e);

                //绘制一条长实线作为标题分界线
                using Pen pen = new Pen(Color.FromArgb(199, 199, 199)) { Width = 1 };

                g.DrawLine(pen, 0, Height - 1, Width, Height - 1);

                int x = 0;
                foreach (ColumnHeader ch in owner.Columns)
                {
                    x = x + ch.Width;
                    Rectangle rectangle = new Rectangle(x - ch.Width, 0, ch.Width - 2, Height - 2);

                    pen.Width = 1;
                    g.DrawLine(pen, x - 1, 0, x - 1, Height - 2);
                    this.DrawString(ch.Text, g, rectangle);
                }
            }
        }

        public class ColumnHeader
        {
            public ColumnHeader()
            {
            }
            [DefaultValue("Name")]
            public string Name { get; set; } = "Name";

            [DefaultValue("CloumnName")]
            public string CloumnName { get; set; } = "CloumnName";
            [DefaultValue("标题")]
            public string Text { get; set; } = "标题";

            [DefaultValue(120)]
            public int Width { get; set; } = 120;

            public override string ToString()
            {
                return $"{Text}[{CloumnName}]";
            }
        }

        public class ColumnHeaderCollection : WenCollection
        {
            public ColumnHeaderCollection(WenTreeViewColumn owner)
            {
                this.owner = owner;
            }

            public ColumnHeader this[int index] => Items.Count <= index ? null : Items[index] as ColumnHeader;

            #region 私有属性
            private readonly WenTreeViewColumn owner;
            #endregion
            public override int Add(object value)
            {
                if (value is ColumnHeader column)
                {
                    owner.Invalidate();
                    WenTreeView.DataListColumn dataListColumn = new WenTreeView.DataListColumn()
                    {
                        Name = column.Name,
                        CloumnName=column.CloumnName,
                        Width = column.Width,
                        Text = column.Text
                    };
                    if (Items.Count == 0)
                        dataListColumn.Width = column.Width - owner.WenTreeView.Indent;

                    owner.WenTreeView.Columns.Add(dataListColumn);
                    return base.Add(value);
                }
                return -1;
            }
            public override void Clear()
            {
                owner.Invalidate();
                base.Clear();
            }
        }
    }
}
